using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.CMS;
using Deepcode.Flux.Core.Systems.CMS.DataTaxonomy;

public partial class Private_CMS_taxonomy_actions_new : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxDTAccess"; } }
	#endregion
	#region Viewstate
	/// <summary>
	/// Gets / Sets the parent node id being managed
	/// </summary>
	public int ParentNodeID
	{
		get
		{
			object o = ViewState["nid"];
			if( o == null ) return -1;
			return (int) o;
		}
		set
		{
			ViewState["nid"] = value;
		}
	}
	#endregion
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			ParentNodeID = GetAttribute("ParentNodeID", -1);

			if (ParentNodeID != -1)
			{
				// Load it
				DataTaxonomyTree t = new DataTaxonomyTree();
				DataTaxonomyNode n = t.GetByID(ParentNodeID);
				if (n == null) GoBack(-1);
			}

			chkSelect.Checked = UserSetting("TAXSELECTNEW", false);
		}
	}
	#endregion
	#region Utilities
	/// <summary>
	/// Returns back to the actions listing, optionally passing 
	/// back a new ID number to select in the tree and actions panel
	/// </summary>
	protected void GoBack()
	{
		GoBack( this.ParentNodeID );
	}
	/// <summary>
	/// Returns back to the actions listing, optionally passing 
	/// back a new ID number to select in the tree and actions panel
	/// </summary>
	protected void GoBack(int NewSelectID)
	{
		Response.Redirect(
			String.Format("~/admin/private/CMS/taxonomy/actions.aspx?parentid={0}&refresh=true", NewSelectID), true);
	}
	#endregion
	#region Interactions

	protected void BtnSave_Clicked(object sender, EventArgs e)
	{
		if( ! Page.IsValid ) return;
		
		DataTaxonomyNode n = new DataTaxonomyNode();
		n.FK_ParentID = this.ParentNodeID;
		n.Name = this.fCategoryName.Text;

		DataTaxonomyTree t = new DataTaxonomyTree();
		t.Save( n );

		SetUserSetting( "TAXSELECTNEW", chkSelect.Checked );
		if( chkSelect.Checked )
			GoBack( n.PK_ID );
		else
			GoBack( this.ParentNodeID );
	}

	protected void BtnCancel_Clicked(object sender, EventArgs e)
	{
		GoBack();
	}
	#endregion
	#region Custom Validator
	/// <summary>
	/// Ensures that the category name specified doesn't exist 
	/// under a different ID at the same sibling level.
	/// </summary>
	/// <param name="source"></param>
	/// <param name="args"></param>
	protected void EnsureUnique(object source, ServerValidateEventArgs args)
	{
		DataTaxonomyTree t = new DataTaxonomyTree();
		DataTaxonomyNode n = t.GetByNameAndParent( this.ParentNodeID, args.Value );
		if(n != null) args.IsValid = false;
		else args.IsValid = true;
	}
	#endregion
}
